local biting = fk.CreateSkill {
  name = "tyj_heg__biting",
}
local H = require "packages.ol_hegemony.util"
local U = require "packages.utility.utility"

Fk:loadTranslationTable {
  ["tyj_heg__biting"] = "蔽庭",
  [":tyj_heg__biting"] = "其他角色获得你的牌后，你可以令其视为对其攻击范围内至多等量名由你选择的角色使用【调虎离山】。",
  ["#tyj_heg__biting-invoke"] = "是否对 %dest 发动 蔽庭 ？",
  ["#tyj_heg__biting-ask"] = "是否发动 蔽庭 视为对其攻击范围内对至多 %arg 名角色使用【调虎离山】",
  ["$tyj_heg__biting1"] = "陛下勿忧，大将军危言耸听。",
  ["$tyj_heg__biting2"] = "陛下，莫让他人知晓此事！",
}
biting:addEffect(fk.AfterCardsMove, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(biting.name) then
      for _, move in ipairs(data) do
        if move.from == player and move.to and move.to ~= player and move.toArea == Card.PlayerHand then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
              return true
            end
          end
        end
      end
    end
  end,
  on_trigger = function(self, event, target, player, data)
    local room = player.room
    local tos = {}
    for _, move in ipairs(data) do
      if move.from == player and move.to and move.to ~= player and move.toArea == Card.PlayerHand then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
            table.insertIfNeed(tos, move.to)
            break
          end
        end
      end
    end
    room:sortByAction(tos)
    for _, to in ipairs(tos) do
      if not player:hasSkill(biting.name) then break end
      if to and not to.dead then
        local num = 0
        for _, move in ipairs(data) do
          if move.from == player and move.to and move.to == to and move.toArea == Card.PlayerHand then
            num = #move.moveInfo
          end
        end
        event:setCostData(self, { tos = { to }, num = num })
        self:doCost(event, to, player, data)
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local target = event:getCostData(self).tos[1]
    local num = event:getCostData(self).num
    local slash = Fk:cloneCard("lure_tiger")
    slash.skillName = "tyj_heg__biting"
    local targets = table.filter(room.alive_players,
      function(p) return target:inMyAttackRange(p) and target:canUseTo(slash, p) and p ~= room.current end)
    if #targets == 0 then return false end
    local tos = room:askToChoosePlayers(player, {
      targets = targets,
      min_num = 1,
      max_num = num,
      prompt = "#tyj_heg__biting-ask:::" .. num,
      skill_name = biting.name,
      cancelable = true,
    })
    if #tos > 0 then
      event:setCostData(self, { usetargets = tos })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:useVirtualCard("lure_tiger", nil, room.current, event:getCostData(self).usetargets, biting.name)
  end,
})
return biting
